home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / mac / files / t_sys5 / 92052tar.gz / 920528.tar / axclient.c < prev    next >
C/C++ Source or Header  |  1991-06-04  |  4KB  |  183 lines

  1. /* @(#) $Header: axclient.c,v 1.10 91/06/04 11:33:41 deyke Exp $ */
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5.  
  6. #include "global.h"
  7. #include "config.h"
  8. #include "netuser.h"
  9. #include "mbuf.h"
  10. #include "timer.h"
  11. #include "ax25.h"
  12. #include "lapb.h"
  13. #include "session.h"
  14.  
  15. static void axclient_parse __ARGS((char *buf, int n));
  16. static void axclient_state_upcall __ARGS((struct ax25_cb *cp, int oldstate, int newstate));
  17.  
  18. /*---------------------------------------------------------------------------*/
  19.  
  20. static void axclient_parse(buf, n)
  21. char *buf;
  22. int n;
  23. {
  24.   if (!(Current && Current->type == AX25TNC && Current->cb.ax25)) return;
  25.   if (n >= 1 && buf[n-1] == '\n') n--;
  26.   if (!n) return;
  27.   send_ax(Current->cb.ax25, qdata(buf, n));
  28.   if (Current->record) {
  29.     if (buf[n-1] == '\r') buf[n-1] = '\n';
  30.     fwrite(buf, 1, n, Current->record);
  31.   }
  32. }
  33.  
  34. /*---------------------------------------------------------------------------*/
  35.  
  36. void axclient_send_upcall(cp, cnt)
  37. struct ax25_cb *cp;
  38. int  cnt;
  39. {
  40.  
  41.   char  *p;
  42.   int  chr;
  43.   struct mbuf *bp;
  44.   struct session *s;
  45.  
  46.   if (!(s = (struct session *) cp->user) || !s->upload || cnt <= 0) return;
  47.   if (!(bp = alloc_mbuf(cnt))) return;
  48.   p = bp->data;
  49.   while (cnt) {
  50.     if ((chr = getc(s->upload)) == EOF) break;
  51.     if (chr == '\n') chr = '\r';
  52.     *p++ = chr;
  53.     cnt--;
  54.   }
  55.   if (bp->cnt = p - bp->data)
  56.     send_ax(cp, bp);
  57.   else
  58.     free_p(bp);
  59.   if (cnt) {
  60.     fclose(s->upload);
  61.     s->upload = 0;
  62.     free(s->ufile);
  63.     s->ufile = 0;
  64.   }
  65. }
  66.  
  67. /*---------------------------------------------------------------------------*/
  68.  
  69. void axclient_recv_upcall(cp, cnt)
  70. struct ax25_cb *cp;
  71. int  cnt;
  72. {
  73.  
  74.   int  c;
  75.   struct mbuf *bp;
  76.  
  77.   if (!(Mode == CONV_MODE && Current && Current->type == AX25TNC && Current->cb.ax25 == cp)) return;
  78.   recv_ax(cp, &bp, 0);
  79.   while ((c = PULLCHAR(&bp)) != -1) {
  80.     if (c == '\r') c = '\n';
  81.     putchar(c);
  82.     if (Current->record) putc(c, Current->record);
  83.   }
  84. }
  85.  
  86. /*---------------------------------------------------------------------------*/
  87.  
  88. static void axclient_state_upcall(cp, oldstate, newstate)
  89. struct ax25_cb *cp;
  90. int  oldstate, newstate;
  91. {
  92.   int  notify;
  93.  
  94.   notify = (Current && Current->type == AX25TNC && Current == (struct session *) cp->user);
  95.   if (newstate != DISCONNECTED) {
  96.     if (notify) printf("%s\n", ax25states[newstate]);
  97.   } else {
  98.     if (notify) printf("%s (%s)\n", ax25states[newstate], ax25reasons[cp->reason]);
  99.     if (cp->user) freesession((struct session *) cp->user);
  100.     del_ax(cp);
  101.     if (notify) cmdmode();
  102.   }
  103. }
  104.  
  105. /*---------------------------------------------------------------------------*/
  106.  
  107. int  doconnect(argc, argv, p)
  108. int  argc;
  109. char  *argv[];
  110. void *p;
  111. {
  112.  
  113.   char  *ap;
  114.   char  path[10*AXALEN];
  115.   struct session *s;
  116.  
  117.   argc--;
  118.   argv++;
  119.   for (ap = path; argc > 0; argc--, argv++) {
  120.     if (!strncmp("via", *argv, strlen(*argv))) continue;
  121.     if (ap > path + sizeof(path) - 1) {
  122.       printf("Too many digipeaters (max 8)\n");
  123.       return 1;
  124.     }
  125.     if (setcall(ap, *argv)) {
  126.       printf("Invalid call \"%s\"\n", *argv);
  127.       return 1;
  128.     }
  129.     if (ap == path) {
  130.       ap += AXALEN;
  131.       addrcp(ap, Mycall);
  132.     }
  133.     ap += AXALEN;
  134.   }
  135.   if (ap < path + 2 * AXALEN) {
  136.     printf("Missing call\n");
  137.     return 1;
  138.   }
  139.   ap[-1] |= E;
  140.   if (!(s = newsession())) {
  141.     printf("Too many sessions\n");
  142.     return 1;
  143.   }
  144.   Current = s;
  145.   s->type = AX25TNC;
  146.   s->name = NULLCHAR;
  147.   s->cb.ax25 = NULLAXCB;
  148.   s->parse = axclient_parse;
  149.   if (!(s->cb.ax25 = open_ax(path, AX_ACTIVE, axclient_recv_upcall, axclient_send_upcall, axclient_state_upcall, (char *) s))) {
  150.     freesession(s);
  151.     switch (Net_error) {
  152.     case NONE:
  153.       printf("No error\n");
  154.       break;
  155.     case CON_EXISTS:
  156.       printf("Connection already exists\n");
  157.       break;
  158.     case NO_CONN:
  159.       printf("Connection does not exist\n");
  160.       break;
  161.     case CON_CLOS:
  162.       printf("Connection closing\n");
  163.       break;
  164.     case NO_MEM:
  165.       printf("No memory\n");
  166.       break;
  167.     case WOULDBLK:
  168.       printf("Would block\n");
  169.       break;
  170.     case NOPROTO:
  171.       printf("Protocol or mode not supported\n");
  172.       break;
  173.     case INVALID:
  174.       printf("Invalid arguments\n");
  175.       break;
  176.     }
  177.     return 1;
  178.   }
  179.   go(argc, argv, p);
  180.   return 0;
  181. }
  182.  
  183.